package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.entity.rank.UnionRank;
import com.yanqu.road.entity.rank.UserRank;
import com.yanqu.road.entity.rank.cross.CrossActivityTeamRank;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.rank.stronghold.CrossActivityUnionRank;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

public class CrossRankPb {

    public static RankProto.RankTempMsg.Builder parseRankTempMsg(CrossUserRank userRank){
        if(null == userRank){
            return null;
        }
        if(0 == userRank.getUserId()){
            return null;
        }
        RankProto.RankTempMsg.Builder msg = RankProto.RankTempMsg.newBuilder();
        msg.setUserId(userRank.getUserId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(userRank.getUserBaseInfo());
        if(null != baseMsg) {
            msg.setPlayerBaseData(baseMsg);
        }
        msg.setValue(userRank.getValue().toString());
        msg.setParam(userRank.getParam());
        return msg;
    }

    public static RankProto.RankListMsg.Builder parseRankListMsg(List<CrossUserRank> userRankList){
        RankProto.RankListMsg.Builder listMsg = RankProto.RankListMsg.newBuilder();
        if(null != userRankList){
            for(CrossUserRank userRank : userRankList){
                if (userRank.isDelete()) {
                    continue;
                }
                RankProto.RankTempMsg.Builder msg = parseRankTempMsg(userRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                }
            }
        }
        return listMsg;
    }

    public static RankProto.RankListMsg.Builder parseRankListMsg(List<CrossUserRank> userRankList,int rankNum){
        int size = 0;
        RankProto.RankListMsg.Builder listMsg = RankProto.RankListMsg.newBuilder();
        if(null != userRankList){
            for(CrossUserRank userRank : userRankList){
                if (userRank.isDelete()) {
                    continue;
                }
                RankProto.RankTempMsg.Builder msg = parseRankTempMsg(userRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                    size++;
                    if (size >= rankNum) {
                        break;
                    }
                }
            }
        }
        return listMsg;
    }

    public static RankProto.RankTempMsg.Builder parseRankTempMsg(UserRank userRank){
        if(null == userRank){
            return null;
        }
        RankProto.RankTempMsg.Builder msg = RankProto.RankTempMsg.newBuilder();
        msg.setUserId(userRank.getUserId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(userRank.getUserBaseInfo());
        if(null != baseMsg) {
            msg.setPlayerBaseData(baseMsg);
        }
        msg.setValue(userRank.getValue().toString());
        msg.setParam(userRank.getParam());
        return msg;
    }

    public static CrossUserRank parseCrossUserRank(int type, RankProto.RankTempMsg rankMsg, int bigRankType) {
        CrossUserRank crossUserRank = new CrossUserRank();
        crossUserRank.setType(type);
        crossUserRank.setUserId(rankMsg.getUserId());
        crossUserRank.setBigRankType(bigRankType);
        UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(rankMsg.getPlayerBaseData());
        crossUserRank.setUserBaseInfo(userBaseInfo);
        crossUserRank.setValue(new BigInteger(rankMsg.getValue()));
        crossUserRank.setParam(rankMsg.getParam());
        if(rankMsg.hasLastUpdateTime()){
            crossUserRank.setLastUpdateTime(rankMsg.getLastUpdateTime());
        }
        return crossUserRank;
    }

    public static UnionActivityRank parseUnionActivityRank(int activityId, UnionActivityRankProto.UnionActivityRankTempMsg rankMsg) {
        UnionActivityRank unionRank = new UnionActivityRank();
        unionRank.setActivityId(activityId);
        unionRank.setUnionUid(rankMsg.getUnionUid());
        UnionBaseInfo unionBaseInfo = parseUnionBaseInfo(rankMsg.getUnionBaseData());
        unionRank.setUnionBaseInfo(unionBaseInfo);
        BigInteger value = BigInteger.ZERO;
        if(!StringUtils.isNullOrEmpty(rankMsg.getValue())){
            value = new BigInteger(rankMsg.getValue());
        }
        unionRank.setValue(value);
        return unionRank;
    }

    public static UnionRank parseUnionRank(RankProto.UnionRankTempMsg rankMsg) {
        UnionRank unionRank = new UnionRank();
        unionRank.setUnionUid(rankMsg.getUnionUid());
        UnionBaseInfo unionBaseInfo = parseUnionBaseInfo(rankMsg.getUnionBaseData());
        unionRank.setUnionBaseInfo(unionBaseInfo);
        return unionRank;
    }

    public static UnionBaseInfo parseUnionBaseInfo(UnionProto.UnionBaseTempMsg unionBaseTempMsg){
        UnionBaseInfo unionBaseInfo = new UnionBaseInfo();
        unionBaseInfo.setUnionAliasId(unionBaseTempMsg.getUnionAliasId());
        unionBaseInfo.setUnionName(unionBaseTempMsg.getUnionName());
        if(unionBaseTempMsg.getUnionFlagCount() > 1){
            unionBaseInfo.setUnionFlagStr(StringUtils.listToString(unionBaseTempMsg.getUnionFlagList(), StringUtils.SEPARATOR));
        }else {
            unionBaseInfo.setUnionFlag(unionBaseTempMsg.getUnionFlag(0));
        }
        unionBaseInfo.setExperience(unionBaseTempMsg.getExperience());
        unionBaseInfo.setMemberCount(unionBaseTempMsg.getMemberCount());
        unionBaseInfo.setAllowRandomJoin(unionBaseTempMsg.getIsAllowRandomJoin());
        unionBaseInfo.setTotalEarnSpeed(new BigInteger(unionBaseTempMsg.getTotalEarnSpeed()));
        unionBaseInfo.setMasterUserId(unionBaseTempMsg.getMasterUserId());
        unionBaseInfo.setMasterNickName(unionBaseTempMsg.getMasterNickName());
        unionBaseInfo.setMasterVipExp(unionBaseTempMsg.getMasterVipExp());
        unionBaseInfo.setMasterDecoration(unionBaseTempMsg.getMasterDecoration());
        unionBaseInfo.setServerId(unionBaseTempMsg.getServerId());
        unionBaseInfo.setHideVipAndAddress(unionBaseTempMsg.getHideMasterVipAndAddress());
        unionBaseInfo.setHonors(unionBaseTempMsg.getHonerPoint());
        unionBaseInfo.setCross(unionBaseTempMsg.getCross());
        return unionBaseInfo;
    }


    public static CrossActivityUnionRank parseCrossUnionActivityRank(int activityId, UnionActivityRankProto.UnionActivityRankTempMsg rankMsg) {
        CrossActivityUnionRank unionRank = new CrossActivityUnionRank();
        unionRank.setActivityId(activityId);
        unionRank.setUnionUid(rankMsg.getUnionUid());
        UnionBaseInfo unionBaseInfo = parseUnionBaseInfo(rankMsg.getUnionBaseData());
        unionRank.setUnionBaseInfo(unionBaseInfo);
        BigInteger value = BigInteger.ZERO;
        if(!StringUtils.isNullOrEmpty(rankMsg.getValue())){
            value = new BigInteger(rankMsg.getValue());
        }
        unionRank.setValue(value);
        return unionRank;
    }

    public static CrossActivityProto.TeamRankListMsg.Builder parseTeamRankListMsg(List<CrossActivityTeamRank> teamRankList){
        CrossActivityProto.TeamRankListMsg.Builder listMsg = CrossActivityProto.TeamRankListMsg.newBuilder();
        if(null != teamRankList){
            for(CrossActivityTeamRank teamRank : teamRankList){
                if (teamRank.isDelete()) {
                    continue;
                }
                CrossActivityProto.TeamRankTempMsg.Builder msg = parseTeamRankTempMsg(teamRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                }
            }
        }
        return listMsg;
    }

    private static CrossActivityProto.TeamRankTempMsg.Builder parseTeamRankTempMsg(CrossActivityTeamRank teamRank) {
        CrossActivityProto.TeamRankTempMsg.Builder tempMsg = CrossActivityProto.TeamRankTempMsg.newBuilder();
        CrossActivityProto.TeamBaseTempMsg.Builder builder = CrossActivityProto.TeamBaseTempMsg.newBuilder();
        builder.setTeamName(teamRank.getTeamBaseInfo().getTeamName());
        tempMsg.setTeamId(teamRank.getTeamId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(teamRank.getTeamBaseInfo().getUserBaseInfo());
        if(null != baseMsg) {
            builder.setPlayerBaseData(baseMsg);
        }
        builder.setCaptainId(teamRank.getTeamBaseInfo().getCaptainId());
        tempMsg.setValue(teamRank.getValue().toString());
        tempMsg.setParam(teamRank.getParam());
        tempMsg.setTeamBaseData(builder);
        return tempMsg;
    }
}
